package cn.edu.dgut.sai;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

/**
 * Hello world!
 */
@SuppressWarnings({"unused", "Convert2MethodRef", "RedundantIfStatement", "SimplifyStreamApiCallChains", "ResultOfMethodCallIgnored"})
public class App {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

    /**
     * filter方法
     * 过滤所有的男性
     */
    public static void fiterSex() {
        List<PersonModel> data = Data.getData();

        //old
        List<PersonModel> temp = new ArrayList<>();
        for (PersonModel person : data) {
            if ("男".equals(person.getSex())) {
                temp.add(person);
            }
        }
        System.out.println(temp);

        //new
        // @formatter:off
        List<PersonModel> collect = data
                                        .stream()
                                        .filter(person -> "男".equals(person.getSex()))
                                        .collect(toList());
        // @formatter:on
        System.out.println(collect);
    }

    /**
     * filter方法
     * 过滤所有的男性 并且小于20岁
     */
    public static void fiterSexAndAge() {
        List<PersonModel> data = Data.getData();

        //old
        List<PersonModel> temp = new ArrayList<>();
        for (PersonModel person : data) {
            if ("男".equals(person.getSex()) && person.getAge() < 20) {
                temp.add(person);
            }
        }
        System.out.println(temp);

        //new 1
        // @formatter:off
        List<PersonModel> collect = data
                                        .stream()
                                        .filter(person -> {
                                            if ("男".equals(person.getSex()) && person.getAge() < 20) {
                                                return true;
                                            }
                                            return false;
                                        })
                                        .collect(toList());
        // @formatter:on

        //new 2
        // @formatter:off
        List<PersonModel> collect1 = data
                                        .stream()
                                        .filter(person -> ("男".equals(person.getSex()) && person.getAge() < 20))
                                        .collect(toList());
        // @formatter:on
    }

    /**
     * map方法
     * 取出所有的用户名字
     */
    public static void getUserNameList() {
        List<PersonModel> data = Data.getData();

        //old
        List<String> list = new ArrayList<>();
        for (PersonModel persion : data) {
            list.add(persion.getName());
        }
        System.out.println(list);

        //new 1
        // @formatter:off
        List<String> collect = data
                                    .stream()
                                    .map(person -> person.getName())
                                    .collect(toList());
        System.out.println(collect);
        // @formatter:on

        //new 2
        // @formatter:off
        List<String> collect1 = data
                                    .stream()
                                    .map(PersonModel::getName)
                                    .collect(toList());
        System.out.println(collect1);
        // @formatter:on

        //new 3
        // @formatter:off
        List<String> collect2 = data
                                    .stream()
                                    .map(person -> {
                                                    System.out.println(person.getName());
                                                    return person.getName();
                                    })
                                    .collect(toList());
        System.out.println(collect2);
        // @formatter:on
    }

    /**
     * flatmap方法
     */
    public static void flatMapString() {
        List<PersonModel> data = Data.getData();
        //返回类型不一样
        List<String> collect = data.stream()
                .flatMap(person -> Arrays.stream(person.getName().split(" "))).collect(toList());

        List<Stream<String>> collect1 = data.stream()
                .map(person -> Arrays.stream(person.getName().split(" "))).collect(toList());

        //用map实现
        List<String> collect2 = data.stream()
                .map(person -> person.getName().split(" "))
                .flatMap(Arrays::stream).collect(toList());
        //另一种方式
        List<String> collect3 = data.stream()
                .map(person -> person.getName().split(" "))
                .flatMap(str -> Arrays.asList(str).stream()).collect(toList());
    }


    /*
     * Collect
     */

    /**
     * toList
     */
    public static void toListTest() {
        List<PersonModel> data = Data.getData();
        List<String> collect = data.stream().map(PersonModel::getName).collect(Collectors.toList());
    }

    /**
     * toSet
     */
    public static void toSetTest() {
        List<PersonModel> data = Data.getData();
        Set<String> collect = data.stream()
                .map(PersonModel::getName)
                .collect(Collectors.toSet());
    }

    /**
     * toMap
     */
    public static void toMapTest() {
        List<PersonModel> data = Data.getData();
        Map<String, Integer> collect = data.stream()
                .collect(
                        Collectors.toMap(PersonModel::getName, PersonModel::getAge)
                );

        data.stream()
                .collect(Collectors.toMap(per -> per.getName(), value -> value + "1"));
    }

    /**
     * 指定类型
     */
    public static void toTreeSetTest() {
        List<PersonModel> data = Data.getData();
        TreeSet<PersonModel> collect = data.stream()
                                            .collect(Collectors.toCollection(TreeSet::new));
        System.out.println(collect);
    }

    /**
     * 分组
     */
    public static void toGroupTest() {
        List<PersonModel> data = Data.getData();
        Map<Boolean, List<PersonModel>> collect = data.stream()
                .collect(Collectors.groupingBy(per -> "男".equals(per.getSex())));
        System.out.println(collect);
    }

    /**
     * 分隔
     */
    public static void toJoiningTest() {
        List<PersonModel> data = Data.getData();
        String collect = data.stream()
                .map(personModel -> personModel.getName())
                .collect(Collectors.joining(",", "{", "}"));
        System.out.println(collect);
    }

    /**
     * 自定义
     */
    public static void reduce() {
        List<String> collect = Stream.of("1", "2", "3")
                .collect(
                        Collectors.reducing(new ArrayList<>(), x -> Collections.singletonList(x), (y, z) -> {
                                    y.addAll(z);
                                    return y;
                                }
                        ));
        System.out.println(collect);
    }
}
